#include "gpio_x.h"
#include "esp_log.h"

#include "m_config.h"

static const char *TAG = "m_led_debug";

#define LED_PIN_POWER GPIO_NUM_18
#define LED_PIN_REC GPIO_NUM_10
#define LED_PIN_LOWBAT GPIO_NUM_23

// #define GPIO_OUPUT_PIN_SEL (1UL << LED_PIN)

void btn_init(gpio_num_t gpio_num){

    assert(GPIO_IS_VALID_GPIO(gpio_num));
    gpio_config_t io_conf = {
        .pin_bit_mask = BIT64(gpio_num),
        .mode = GPIO_MODE_DISABLE,
        //for powersave reasons, the GPIO should not be floating, select pullup
        .pull_up_en = true,
        .pull_down_en = false,
        .intr_type = GPIO_INTR_DISABLE,
    };    //default setting, coppied from gpio_reset_pin()

    // change some as your need
    //interrupt of falling edge
    io_conf.intr_type = GPIO_INTR_NEGEDGE;
    //set as input mode
    io_conf.mode = GPIO_MODE_INPUT;
    //enable pull-up mode
    io_conf.pull_up_en = 1;
    gpio_config(&io_conf);
}

int get_btn_level(gpio_num_t gpio_num){
    return gpio_get_level(gpio_num);
}

#ifdef AMP_EN_PIN
void amp_pin_init(){
    gpio_config_t io_conf = {
        .pin_bit_mask = (1ULL<<AMP_EN_PIN),
        .mode = GPIO_MODE_DISABLE,
        //for powersave reasons, the GPIO should not be floating, select pullup
        .pull_up_en = true,
        .pull_down_en = false,
        .intr_type = GPIO_INTR_DISABLE,
    };    //default setting, coppied from gpio_reset_pin()

    // change some as your need
    io_conf.mode = GPIO_MODE_INPUT_OUTPUT;
    //enable pull-up mode
    io_conf.pull_down_en = true;
    gpio_config(&io_conf);
}


void amp_enable(void){
    gpio_set_level(AMP_EN_PIN,1);
}

void amp_disable(void){
    gpio_set_level(AMP_EN_PIN,0);
}
#endif

#ifndef USE_SPI_DATA_OUT_TMP_SOLUTION 

void led_init(void){
        gpio_config_t io_conf = {
        .pin_bit_mask = ((1UL << LED_PIN_REC) | (1UL << LED_PIN_POWER) | (1UL << LED_PIN_LOWBAT)) ,        
        .mode = GPIO_MODE_INPUT_OUTPUT,
        .intr_type = GPIO_INTR_DISABLE,
        .pull_down_en = 0,
        .pull_up_en = 1};
    gpio_config(&io_conf);
    
    ESP_LOGI(TAG, "led_init");
}

/* gpio_get_level not work, gpio_get_level used in input pin, output pin always return 0 
int get_led_state(gpio_num_t gpio_num){
    return gpio_get_level(gpio_num);
}
void led_toggle(gpio_num_t gpio_num){
    set_led_level(gpio_num, 1-get_led_state(gpio_num));    
}
void keep_power_led_on(void){
    if (gpio_get_level(LED_PIN_POWER)==0)
        return;
    if (gpio_get_level(LED_PIN_POWER)==1)
        power_led_on();
}
*/


//  - - - ------------- recording led
void rec_led_toggle(void){
    static bool state = 0;  
    state = !state;
    gpio_set_level(LED_PIN_REC,state);
}

void rec_led_on(void){
    gpio_set_level(LED_PIN_REC, 0);    
}

void rec_led_off(void){
    gpio_set_level(LED_PIN_REC, 1);    
}

//  - - - ------------- power led

static bool power_led_state = 0;  // 1 means led off; 0 means on led on

void power_led_toggle(void){
    if (power_led_state==0){
        power_led_state = 1;
        gpio_set_level(LED_PIN_POWER,power_led_state);//turn off
    }else{
        power_led_state = 0;
        gpio_set_level(LED_PIN_POWER,power_led_state);//turn on
    }
}

void power_led_on(void){
    gpio_set_level(LED_PIN_POWER, 0);    
}

bool get_power_led_state(void){
    return power_led_state;
}

void keep_power_led_on(void){
    if (power_led_state==1){
        power_led_toggle();
    }
}

//  - - - ------------- low battery led
void lowbattery_led_on(void){
    gpio_set_level(LED_PIN_LOWBAT, 0);    
}

void lowbattery_led_off(void){
    gpio_set_level(LED_PIN_LOWBAT, 1);    
}

void lowbattery_toggle(void){     
    static bool state = 0;  
    state = !state;
    gpio_set_level(LED_PIN_LOWBAT,state);
}


// if use USE_SPI_DATA_OUT_TMP_SOLUTION, then use these led pins to connect to stm32
#endif // USE_SPI_DATA_OUT_TMP_SOLUTION
